<?php

/**
 * This is the model class for table "slider_banner".
 *
 * The followings are the available columns in table 'slider_banner':
 * @property integer $id
 * @property string $name
 * @property string $image
 * @property string $type
 * @property string $imageLink
 * @property string $image_link_target
 * @property string $infoText
 * @property string $infoTextLink
 * @property string $infoTextLinkTitle
 * @property string $info_text_link_target
 * @property integer $sort
 * @property integer $isActive
 */
class SliderBanner extends CActiveRecord
{
    protected $imageName;

	public function tableName()
	{
		return 'sliderBanner';
	}

	public function rules()
	{
		return array(
			array('image_id', 'required'),
			array('sort, is_active', 'numerical', 'integerOnly'=>true),
			array('name, image_link, info_text, info_text_link', 'length', 'max'=>255),
			array('type', 'length', 'max'=>50),
			array('image_link_target, info_text_link_target', 'length', 'max'=>20),
			array('info_text_link_title', 'length', 'max'=>100),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, image_id, type, image_link, image_link_target, info_text, info_text_link, info_text_link_title, info_text_link_target, sort, is_active', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
            'image'=>array(self::BELONGS_TO, 'File', 'image_id'),
		);
	}

    public function behaviors(){
        return array(
            'lang'=>array(
                'class'=>'MultiLanguageBehavior',
                //'imagePath'=>'upload/images/posts',
                //'imageField'=>'image',
            ),
        );
    }

	public function attributeLabels()
	{
		return array(
            'id'=>Yii::t('main','id'),
            'name'=>Yii::t('main','name'),
            'image'=>Yii::t('main','image'),

            'type'=>Yii::t('slider','type'),
            'imageLink'=>Yii::t('slider','imageLink'),
            'image_link_target'=>Yii::t('slider','imageLinkType'),
            'infoText'=>Yii::t('slider','textInfoBlock'),
            'infoTextLink'=>Yii::t('slider','linkInfoBlock'),
            'infoTextLinkTitle'=>Yii::t('slider','nameLinkInfoBlock'),
            'info_text_link_target'=>Yii::t('slider','typeLinkInfoBlock'),
            'sort'=>Yii::t('main','sortOrder'),
            'isActive'=>Yii::t('main','isActive'),
		);
	}

    // Getters

    public function getSlideName($code='')
    {
        $result=$this->lang->translate('name',$code);
        return $result ? $result : $this->name;
    }

    public function getPicture()
    {
        return '<img src="'.$this->image->imageUrl.'" />';
    }

    public function getImageName()
    {
        return Yii::app()->params['uploadImagePath'].$this->image->fileName;
    }

    public function getImageLink()
    {
        return $this->image_link;
    }

    public function getInfoText()
    {
        $result=$this->lang->translate('info_text');
        return $result ? $result : $this->info_text;
    }

    public function getInfoTextLink()
    {
        return $this->info_text_link;
    }

    public function getInfoTextLinkTitle()
    {
        $result=$this->lang->translate('info_text_link_title');
        return $result ? $result : $this->info_text_link_title;
    }

    public function getIsActive()
    {
        return $this->is_active;
    }

    // END Getters


    // Setters

    public function setSlideName($name)
    {
        $this->name=$this->lang->setMultiLangValues('name',$this,$name);
    }

    public function setImageName($image)
    {
        $this->image_id=Yii::app()->file->saveImage($this,'imageName');
    }

    public function setInfoText($text)
    {
        $this->info_text=$this->lang->setMultiLangValues('info_text',$this,$text);
    }

    public function setInfoTextLinkTitle($title)
    {
        $this->info_text_link_title=$this->lang->setMultiLangValues('info_text_link_title',$this,$title);
    }

    public function setImageLink($link)
    {
        $this->image_link=$link;
    }

    public function setInfoTextLink($link)
    {
        $this->info_text_link=$link;
    }

    // END Setters

	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('image_id',$this->image_id,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('image_link',$this->image_link,true);
		$criteria->compare('image_link_target',$this->image_link_target,true);
		$criteria->compare('info_text',$this->info_text,true);
		$criteria->compare('info_text_link',$this->info_text_link,true);
		$criteria->compare('info_text_link_title',$this->info_text_link_title,true);
		$criteria->compare('info_text_link_target',$this->info_text_link_target,true);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('is_active',$this->is_active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave()
    {
        return parent::beforeSave();
    }

    public function afterSave()
    {
        if(!$this->lang->afterSave()) return false;
        return parent::afterSave();
    }

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
